
import Sortable from "sortablejs";

export const updateSort = (vexGridRef, gridOptions, key) => {
    setTimeout(() => {
        if (!key) {
            key = 'sort';
        }
        vexGridRef = vexGridRef.value? vexGridRef.value : vexGridRef;
        //el绑定元素需要找到vxe的DOM下的tbody元素
        Sortable.create(vexGridRef.$el.querySelector('.body--wrapper>.vxe-table--body tbody'), {
            animation: 150,//拖拽动画过渡时间
            sort: true,
            handle: '.move', //必须设置该属性，属性值为你要拖动的该单元格的样式名
            // 开始拖拽的时候
            onStart: function (/**Event*/evt) {
                evt.oldIndex;
            },
            onEnd: function ({oldIndex, newIndex, item}) {
                let rowNode = vexGridRef.getRowNode(item);
                newIndex = rowNode.index - oldIndex + newIndex
                let newsArr = getNewArrData(rowNode.index, newIndex, gridOptions.data)

                for (let i = 0; i < newsArr.length; i++) {
                    newsArr[i][key] = i + 1;
                }
                gridOptions.data = newsArr
                gridOptions.updateSortMethod(undefined);
            },
        })
    },100)
}

export const updateSortRowCall = (vexGridRef, call) => {
    let el = vexGridRef.value ? vexGridRef.value.$el : vexGridRef.$el;
    //el绑定元素需要找到vxe的DOM下的tbody元素
    return Sortable.create(el.querySelector('.body--wrapper>.vxe-table--body tbody'), {
        animation: 150,//拖拽动画过渡时间
        onEnd: function ({oldIndex, newIndex, item}) {
            let rowNode = vexGridRef.getRowNode(item);
            newIndex = rowNode.index - oldIndex + newIndex
            call(rowNode.index, newIndex)
        },
    })
}

export const updateSortCall = (vexGridRef, call) => {
    let el = vexGridRef.value ? vexGridRef.value.$el : vexGridRef.$el;
    return this.$nextTick(() => {
        //el绑定元素需要找到vxe的DOM下的tbody元素
        return Sortable.create(el.querySelector('.body--wrapper>.vxe-table--body tbody'), {
            animation: 150,//拖拽动画过渡时间
            sort: true,
            handle: '.move', //必须设置该属性，属性值为你要拖动的该单元格的样式名
            onEnd: function ({oldIndex, newIndex, item}) {
                call(oldIndex, newIndex, item)
            },
        })
    })
}

export const getNewArrData = (oldIndex, newIndex, data, reFiled) => {
    const list = JSON.parse(JSON.stringify(data)) //保存旧数据
    const targetRow = list.splice(oldIndex, 1)[0] //保存移动的元素并删除原列表中移动的元素
    list.splice(newIndex, 0, targetRow) //插入到目标新索引中
    if (reFiled) {
        let start = Math.min(oldIndex, newIndex)
        let end = Math.max(oldIndex, newIndex)
        for (let i = start; i <= end; i++) {
            start[i][reFiled] = data[i][reFiled]
        }
    }
    return JSON.parse(JSON.stringify(list)) //重新赋值数组
}
export const getNewArrDataToolBtn = (oldIndex, newIndex, data, toolBtn) => {
    const list = JSON.parse(JSON.stringify(data)) //保存旧数据
    const targetRow = list.splice(oldIndex, 1)[0] //保存移动的元素并删除原列表中移动的元素
    list.splice(newIndex, 0, targetRow) //插入到目标新索引中
    if (toolBtn) {
        let kw = toolBtn.beforeFunction.split(",");
        let start = Math.min(oldIndex, newIndex)
        let end = Math.max(oldIndex, newIndex)
        for (let i = start; i <= end; i++) {
            list[i][kw[0]] = data[i][kw[0]]
            if (kw.length == 2) {
                toolBtn.updateData[list[i][kw[1]]] = list[i][kw[0]]
            }
        }
    }
    return JSON.parse(JSON.stringify(list)) //重新赋值数组
}
